Skip to content

Mermaid validation: PythonMonkey on main thread, then system Node, never mermaid.ink - #1

Merged
dandydarshan merged 7 commits into
mainfrom
fix/pythonmonkey-main-thread
Oct 28, 2026
Merged

dandydarshan merged 7 commits into
mainfrom
fix/pythonmonkey-main-thread

Conversation

@dandydarshan

@dandydarshan dandydarshan commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Problem

_PYTHONMONKEY_BROKEN = sys.version_info >= (3, 12) skipped PythonMonkey on 3.12+, sending all Mermaid validation to mermaid.ink (diagram content leaves the machine). Testing on Linux and on GitHub's macOS runners (arm64 + Intel, py3.12–3.14, PythonMonkey 1.2.0 and 1.4.1; see the experiment/pythonmonkey-macos branch) shows the crash depends on the thread, not the Python version:

  • import on a non-main thread (caw's FastMCP tool thread) → segfault during validation
  • use from a non-main thread's event loop (web app's worker thread) → segfault at interpreter exit (the original "crash after a successful run")
  • import and use on the main thread → fine on every version/OS tested (30/30 with 1.4.1)

Change

Mermaid validation now runs in this order and never calls a web service:

  1. PythonMonkey, always on the process main thread: directly, or handed to the main loop via run_coroutine_threadsafe (caw backend). The 3.12+ skip is gone; pythonmonkey bumped to 1.4.1.
  2. System Node.js when PythonMonkey is unavailable (e.g. the web app's worker thread, or a failed import). It runs mermaid_parser/js/parser.bundle.js, the bundle mermaid-parser-py already ships and PythonMonkey loads, so parse errors are identical. No npm install, no lock file, nothing extra in the wheel. Node comes from PATH, or CODEWIKI_NODE_PATH.
  3. Skip with one warning if Node is missing, times out or fails. A skipped diagram counts as valid so agents don't loop on "fixing" it.

The mermaid.ink fallback (mermaid-py, MERMAID_VALIDATE) is commented out in codewiki/src/be/utils.py, along with the mermaid-py dependency, so it can be restored by uncommenting. An earlier version of this PR added codewiki/mermaid_node/ with a package.json/package-lock.json and installed mermaid via npm at run time; that's removed, since it wasn't in package-data (missing from wheels) and needed network access and a writable install dir.

Tests

  • tests/test_compiled_wheel.py runs both thread layouts in a subprocess (it segfaults without the fix). The main-thread case checks PythonMonkey is used; the worker-thread case checks PythonMonkey is never loaded and, when node is on PATH, that good/broken diagrams are reported correctly through Node.
  • Full suite: 461 passed locally (Linux, Node 22). Checked by hand: main thread → PythonMonkey; worker thread → Node; Node missing → one warning, then skipped.
  • Not yet run on Windows/macOS or from a built Cython wheel.

🤖 Generated with Claude Code

https://claude.ai/code/session_011nNK4oKT17vZwKhUezJQ5z

Darshan M U and others added 7 commits October 25, 2026 22:50
PythonMonkey crashes Python when it is imported on a non-main thread
(segfault during validation, caw's FastMCP tool thread) or used from a
non-main thread's loop (segfault at exit, the web app's worker thread). This
happens with 1.2.0 and 1.4.1 on Linux and macOS, on every Python version
tested; the old "3.12+ only" diagnosis was wrong, so the version skip just
sent all validation to mermaid.ink.

Now the parse, import included, always runs on the main thread: directly, or
handed to the main loop via run_coroutine_threadsafe. Off the main thread
with no main loop to hand off to, validation falls back to mermaid-py.
Bump pythonmonkey to 1.4.1. The new test runs both thread layouts in a
subprocess, so a segfault fails it instead of killing pytest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…work

The Node fallback ran codewiki/mermaid_node/validate.mjs, which was not in
package-data (so missing from wheels) and npm-installed mermaid into
site-packages at run time (network access, writable install dir, a second
mermaid version). Node now runs mermaid-parser-py's own parser.bundle.js,
the same file PythonMonkey loads: no npm, no lock file, same parse errors.

Drop the mermaid.ink fallback (mermaid-py, MERMAID_VALIDATE). If neither
PythonMonkey nor Node can run, log one warning and skip validation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011nNK4oKT17vZwKhUezJQ5z
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011nNK4oKT17vZwKhUezJQ5z
@dandydarshan
dandydarshan force-pushed the fix/pythonmonkey-main-thread branch from 1f61dbe to e8f5687 Compare October 25, 2026 17:22
@dandydarshan dandydarshan changed the title Use PythonMonkey only on the main thread; drop the Python 3.12+ skip Mermaid validation: PythonMonkey on main thread, then system Node, never mermaid.ink Oct 25, 2026
@dandydarshan
dandydarshan merged commit 25d9a7e into main Oct 28, 2026
13 checks passed
@dandydarshan
dandydarshan deleted the fix/pythonmonkey-main-thread branch October 28, 2026 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants